﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QueryStuMVVM.Models;

namespace QueryStuMVVM.Db
{
    class LocalDb
    {
        public LocalDb()
        {
            Init();
        }

        private void Init()
        {
            StudentList = new List<Student>();
            for (int i = 0; i < 30; ++i)
            {
                StudentList.Add(new Student()
                {
                    Id = i,
                    Name = $"Sample{i}"
                });
            }
        }

        public void AddStudent(Student stu)
        {
            StudentList.Add(stu);
        }

        public void DelStudent(int id)
        {
            var model = StudentList.FirstOrDefault(t => t.Id == id);
            if (model !=null)
            {
                StudentList.Remove(model);
            }
        }

        public List<Student> GetStudentsByName(string name)
        {
            if (name == null)
            {
                return StudentList;
            }

            return StudentList.Where(q=>q.Name.Contains(name)).ToList();
        }

        public Student GetStudentsBId(int id)
        {
            var model = StudentList.FirstOrDefault(q => q.Id == id);
            if (model != null)
            {
                return new Student()
                {
                    Id = model.Id,
                    Name = model.Name
                };
            }

            return null;
        }

        public List<Student> StudentList { get; set; }
    }
}
